﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 就诊人表服务
    /// </summary>
    public class PatientService : IPatientService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public PatientService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="patientName">患者姓名</param>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="idNumber">身份证</param>
        /// <param name="sex">-1全部 1男 0女</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public PatientListDto GetPatientList(string patientName, string phoneNumber, string idNumber, string sex, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            PatientListDto patientListDto = new PatientListDto();
            patientListDto.PatientList = _db.Queryable<PatientList>()
                .WhereIF(patientName != null, x => x.PatientName.Equals(patientName))
                .WhereIF(phoneNumber != null, x => x.PhoneNumber.Equals(phoneNumber))
                .WhereIF(idNumber != null, x => x.IdNumber.Equals(idNumber))
                .WhereIF(sex != null, x => x.Sex.Equals(sex))
                .ToPageList(pageIndex, pageSize, ref totalCount);
            patientListDto.Total = totalCount;
            patientListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return patientListDto;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="patientList">id不用管它</param>
        /// <returns></returns>
        public bool InsertPatient(PatientList patientList)
        {
            if (_db.Insertable(patientList).ExecuteCommand() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 添加前台看病记录
        /// </summary>
        /// <param name="patientRecordList">除了病人id一定要给，其他的无所谓</param>
        /// <returns></returns>
        public bool InsertPatientRecord(PatientRecordList patientRecordList)
        {
            if (_db.Insertable(patientRecordList).ExecuteCommand() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取等待被看病病人的列表
        /// state=2
        /// </summary>
        /// <param name="docId">医生id</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public PatientRecordListDto GetAppList(string docId, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            PatientRecordListDto patientRecordListDto = new PatientRecordListDto();
            patientRecordListDto.PatientRecordList = _db.Queryable<PatientRecordList>()
                .Where(x => x.DocId.Equals(docId))
                .Where(x => x.State == 2)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            patientRecordListDto.Total = totalCount;
            patientRecordListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return patientRecordListDto;
        }

        /// <summary>
        /// 添加医生看病记录
        /// </summary>
        /// <param name="insertDocRecordDto"></param>
        /// <returns></returns>
        public bool InsertDocRecord(InsertDocRecordDto insertDocRecordDto)
        {
            return _db.Updateable<PatientRecordList>()
                .SetColumns(x => x.DocTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                .SetColumns(x => x.DepartmentId == insertDocRecordDto.DepartmentId)
                .SetColumns(x => x.State == insertDocRecordDto.State)
                .SetColumnsIF(insertDocRecordDto.DocDesc != "", x => x.DocDesc == insertDocRecordDto.DocDesc)
                .SetColumnsIF(insertDocRecordDto.DocMedicine != "", x => x.DocMedicine == insertDocRecordDto.DocMedicine)
                .Where(x => x.RecordId == insertDocRecordDto.RecordId)
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 获取等待入院的列表
        /// state=4
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public PatientRecordListDto GetInHospitalList(int pageIndex, int pageSize)
        {
            int totalCount = 0;
            PatientRecordListDto patientRecordListDto = new PatientRecordListDto();
            patientRecordListDto.PatientRecordList = _db.Queryable<PatientRecordList>()
                .Where(x => x.State == 4)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            patientRecordListDto.Total = totalCount;
            patientRecordListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return patientRecordListDto;
        }

        /// <summary>
        /// 根据科室id获取科室下面的空闲中的病床的列表(病床状态是1)
        /// </summary>
        /// <param name="departmentId">科室id</param>
        /// <returns></returns>
        public List<SickBedList> GetSickBedsByDepartmentId(int departmentId)
        {
            return _db.Queryable<SickBedList>()
                .Where(x => x.DepartmentId == departmentId)
                .Where(x => x.SickBedState == 1)
                .ToList();
        }

        /// <summary>
        /// 分配病床
        /// </summary>
        /// <param name="inSickBedDto"></param>
        /// <returns></returns>
        public bool InSickBed(InSickBedDto inSickBedDto)
        {
            if (_db.Updateable<PatientRecordList>()
                .SetColumns(x => x.SickBedId == inSickBedDto.SickBedId)
                .SetColumns(x => x.InHospitalTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                .SetColumns(x => x.NurseId == inSickBedDto.NurseId)
                .SetColumns(x => x.State == 5)
                .Where(x => x.RecordId == inSickBedDto.RecordId)
                .ExecuteCommandHasChange()
                && _db.Updateable<SickBedList>()
                .SetColumns(x => x.SickBedState == 2)
                .Where(x => x.SickBedId == inSickBedDto.SickBedId)
                .ExecuteCommandHasChange())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取看病记录
        /// </summary>
        /// <param name="departmentId">科室id</param>
        /// <param name="patientName">患者姓名</param>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="idNumber">身份证</param>
        /// <param name="sex">1男 0女</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <param name="state">状态 1前台诊断中 2等待医生诊断 3医生诊断中 4办理住院中 5住院中 6已出院</param>
        /// <returns></returns>
        public GetPatientRecordDto GetPatientRecordList(int departmentId, string patientName, string phoneNumber, string idNumber, string sex, int pageIndex, int pageSize, int state)
        {
            int totalCount = 0;
            GetPatientRecordDto getPatientRecordDto = new GetPatientRecordDto();
            getPatientRecordDto.GetPatientRecordListDto = _db.Queryable<PatientList>()
                .LeftJoin<PatientRecordList>((p, pr) => p.PatientId == pr.PatientId)
                .WhereIF(patientName != null, (p, pr) => p.PatientName.Contains(patientName))
                .WhereIF(phoneNumber != null, (p, pr) => p.PhoneNumber.Equals(phoneNumber))
                .WhereIF(idNumber != null, (p, pr) => p.IdNumber.Equals(idNumber))
                .WhereIF(sex != null, (p, pr) => p.Sex.Equals(sex))
                .WhereIF(state != 0, (p, pr) => pr.State == state)
                .WhereIF(departmentId!=-1, (p, pr)=>pr.DepartmentId==departmentId)
                .Select((p, pr) => new GetPatientRecordListDto
                {
                    PatientId = p.PatientId,
                    PatientName = p.PatientName,
                    PhoneNumber = p.PhoneNumber,
                    IdNumber = p.IdNumber,
                    Birth = p.Birth,
                    Sex = p.Sex,
                    RecordId = pr.RecordId,
                    InquiryId = pr.InquiryId,
                    InquiryTime = pr.InquiryTime,
                    InquiryDesc = pr.InquiryDesc,
                    InquiryMedicine = pr.InquiryMedicine,
                    DocId = pr.DocId,
                    DocTime = pr.DocTime,
                    DocDesc = pr.DocDesc,
                    DocMedicine = pr.DocMedicine,
                    DepartmentId = pr.DepartmentId,
                    SickBedId = pr.SickBedId,
                    InHospitalTime = pr.InHospitalTime,
                    NurseId = pr.NurseId,
                    State = pr.State,
                    LeaveHospitalTime = pr.LeaveHospitalTime,
                    IsTransfer = pr.IsTransfer,
                    LeaveHospitalDesc = pr.LeaveHospitalDesc
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);
            getPatientRecordDto.Total = totalCount;
            getPatientRecordDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return getPatientRecordDto;
        }

        /// <summary>
        /// 出院
        /// </summary>
        /// <param name="leaveHopitalDto"></param>
        /// <returns></returns>
        public bool LeaveHopital(LeaveHopitalDto leaveHopitalDto)
        {
            int? sickBedId = _db.Queryable<PatientRecordList>()
                .Where(x => x.RecordId == leaveHopitalDto.RecordId)
                .Select(x => x.SickBedId)
                .First();
            if (_db.Updateable<PatientRecordList>()
                .SetColumns(x=>x.LeaveHospitalDesc == leaveHopitalDto.LeaveHospitalDesc)
                .SetColumns(x=>x.IsTransfer == leaveHopitalDto.IsTransfer)
                .SetColumns(x => x.State == 6)
                .Where(x => x.RecordId == leaveHopitalDto.RecordId)
                .ExecuteCommandHasChange()
                && _db.Updateable<SickBedList>()
                .SetColumns(x => x.SickBedState == 1)
                .Where(x => x.SickBedId == sickBedId)
                .ExecuteCommandHasChange())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 转科
        /// </summary>
        /// <param name="transferDepartmentDto"></param>
        /// <returns></returns>
        public bool TransferDepartment(TransferDepartmentDto transferDepartmentDto)
        {
            //获取当前病床id
            int? sickBedId = _db.Queryable<PatientRecordList>()
                .Where(x => x.RecordId == transferDepartmentDto.IsTransfer)
                .Select(x => x.SickBedId)
                .First();

            PatientRecordList patientRecordList = new PatientRecordList()
            {
                PatientId = transferDepartmentDto.PatientId,
                DepartmentId = transferDepartmentDto.DepartmentId,
                InquiryDesc = transferDepartmentDto.InquiryDesc,
                IsTransfer = transferDepartmentDto.IsTransfer,
                InHospitalTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                State = 4
            };

            if (_db.Insertable(patientRecordList)
                .ExecuteCommand() == 1
                && _db.Updateable<SickBedList>()
                .SetColumns(x => x.SickBedState == 1)
                .Where(x => x.SickBedId == sickBedId)
                .ExecuteCommandHasChange()
                && _db.Updateable<PatientRecordList>()
                .SetColumns(x => x.State == 7)
                .SetColumns(x => x.LeaveHospitalTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                .Where(x => x.RecordId == transferDepartmentDto.IsTransfer)
                .ExecuteCommandHasChange())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
